package com.example.taobaounion.presenter.impl

import android.util.Log
import com.example.taobaounion.model.bean.CategoryPagerData
import com.example.taobaounion.net.API
import com.example.taobaounion.presenter.ICategoryPagerPresenter
import com.example.taobaounion.view.ICategoryPagerCallBack
import com.example.taobaounion.utils.RetrofitManager
import com.example.taobaounion.utils.UrlUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 *@author : Administrator
 *@descreption : viewpager分类的presenter实现类
 */
class CategoryPagerPresenterImpl : ICategoryPagerPresenter {
    private val mCallBackList = ArrayList<ICategoryPagerCallBack>()

/*     constructor()

    companion object {
        private val instance = CategoryPagerPresenterImpl()
        fun instance(CategoryPagerCallBack: ICategoryPagerCallBack): CategoryPagerPresenterImpl {
            return instance
        }
    }*/

    override fun registerCallBack(callBack: ICategoryPagerCallBack) {
        if (!mCallBackList.contains(callBack)) {
            mCallBackList.add(callBack)
        }
    }


    override fun unRegisterCallBack(callBack: ICategoryPagerCallBack) {
        if (mCallBackList.contains(callBack)) {
            mCallBackList.remove(callBack)
        }
    }


    override fun loadData(id: Int, page: Int) {
        mCallBackList.forEach {
            if (page==1){
                it.loading(id)
            }
        }
        val request =
            RetrofitManager.getInstance().retrofit.create(API::class.java)
        val call = request.getCategoryData(UrlUtil.getCategoryDataUrl(id, page))
        call.enqueue(object : Callback<CategoryPagerData> {
            override fun onFailure(call: Call<CategoryPagerData>, t: Throwable) {
                handleLoadErrorEvent(id)
            }


            override fun onResponse(call: Call<CategoryPagerData>, response: Response<CategoryPagerData>) {
                val data = response.body()
                if (data==null){
                    handleLoadErrorEvent(id)
                }else{
                    if (data.data.size==0){
                        noData(id)
                    }else{
                        handleLoadSuccessEvent(id, data.data)
                    }
                }
            }

        })
    }

    private fun handleLoadSuccessEvent(id: Int, data: List<CategoryPagerData.DataBean>) {
        Log.i("TAG", "handleSuccessEvent ${mCallBackList.size}");

        mCallBackList.forEach {
            it.loadSuccess(data, id)
        }
    }

    private fun handleLoadErrorEvent(id: Int) {
        mCallBackList.forEach {
            it.loadError(id)
        }
    }

    override fun reLoad(id: Int) {
        val request =
            RetrofitManager.getInstance().retrofit.create(API::class.java)
        val call = request.getCategoryData(UrlUtil.getCategoryDataUrl(id, 1))
        call.enqueue(object : Callback<CategoryPagerData> {
            override fun onFailure(call: Call<CategoryPagerData>, t: Throwable) {
                Log.i("TAG", "数据返回失败 ");
                handleLoadMoreErrorEvent(id)
            }

            override fun onResponse(call: Call<CategoryPagerData>, response: Response<CategoryPagerData>) {
                Log.i("TAG", "数据返回成功 " + response.body().toString());
                val data = response.body()
                if (data==null){
                    handleLoadMoreErrorEvent(id)
                }else {
                    if (data.data.size==0){
                        noMoreData(id)
                    }else{
                        handleReloadMoreSuccessEvent(id, data.data)
                    }
                }

            }

        })
    }

    private fun handleReloadMoreSuccessEvent(id: Int, data: List<CategoryPagerData.DataBean>) {
        mCallBackList.forEach {
            it.reLoadSuccess(data,id)
        }
    }

    override fun loadMore(id: Int, page: Int) {
        val request =
            RetrofitManager.getInstance().retrofit.create(API::class.java)
        val call = request.getCategoryData(UrlUtil.getCategoryDataUrl(id, page))
        call.enqueue(object : Callback<CategoryPagerData> {
            override fun onFailure(call: Call<CategoryPagerData>, t: Throwable) {
                Log.i("TAG", "数据返回失败 ");
                handleLoadMoreErrorEvent(id)
            }

            override fun onResponse(call: Call<CategoryPagerData>, response: Response<CategoryPagerData>) {
                Log.i("TAG", "数据返回成功 " + response.body().toString());
                val data = response.body()
                if (data==null){
                    handleLoadMoreErrorEvent(id)
                }else {
                    if (data.data.size==0){
                        noMoreData(id)
                    }else{
                        handleLoadMoreSuccessEvent(id, data.data)
                    }
                }

            }

        })
    }

    private fun handleLoadMoreErrorEvent(id: Int) {
        mCallBackList.forEach {
            it.loadMoreError(id)
        }
    }

    private fun handleLoadMoreSuccessEvent(id: Int, data: List<CategoryPagerData.DataBean>) {
        mCallBackList.forEach {
            it.loadMoreSuccess(data,id)
        }
    }

    override fun loadSuccess() {

    }

    override fun loadError() {

    }

     fun noData(id:Int) {
        mCallBackList.forEach {
            it.noData()
        }
    }
    fun noMoreData(id:Int) {
        mCallBackList.forEach {
            it.noMoreData()
        }
    }
}